home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / other_examples / PubSc / igadgets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  2.8 KB  |  139 lines

  1. /* igadgets.c -- common gadget building utilities    :ts=4 */
  2.  
  3. /*
  4. Copyright (c) 1989 Commodore-Amiga, Inc.
  5.  
  6. Executables based on this information may be used in software
  7. for Commodore Amiga computers. All other rights reserved.
  8. This information is provided "as is"; no warranties are made.
  9. All use is at your own risk, and no liability or responsibility
  10. is assumed.
  11. */
  12.  
  13. #include "sysall.h"
  14.  
  15. #define D(x)    ;
  16. #define DF(x)    ;
  17.  
  18. struct Gadget    *CreateGadget();
  19.  
  20. /*
  21.  * doesn't setup Activation field, except for BOOLEXTEND
  22.  * image parameter is required
  23.  */
  24. struct Gadget *
  25. CreateBoolGadget( image, mask, id, str )
  26. struct Image    *image;
  27. UWORD            *mask;
  28. int                id;            /* user ID    */
  29. UBYTE            *str;        /* create intuitext, position 0,0 */
  30. {
  31.     struct Gadget    *g = NULL;
  32.     struct BoolInfo    *bi;
  33.     int                bisize;
  34.  
  35.     bisize = mask? (sizeof (struct BoolInfo)): 0;
  36.  
  37.     if (image &&    
  38.         (g=CreateGadget(image->Width, image->Height, id, str, bisize)))
  39.     {
  40.         g->GadgetType = BOOLGADGET;
  41.         g->Flags = GADGHCOMP | GADGIMAGE;
  42.         g->GadgetRender = (APTR) image;
  43.  
  44.         /* init BoolInfo    */
  45.         if ( mask )
  46.         {
  47.             bi = (struct BoolInfo *) g->SpecialInfo;
  48.             D( printf("CBG: mask at %lx bi: %lx\n", mask, bi ));
  49.             g->Activation |= BOOLEXTEND;
  50.             bi->Flags = BOOLMASK;
  51.             bi->Mask = mask;
  52.             bi->Reserved = 0;
  53.         }
  54.     }
  55.  
  56.     D( printf("CBG returns %lx\n", g ) );
  57.     return ( g );
  58. }
  59.  
  60.  
  61. #define GTSIZE (sizeof(struct Gadget)+sizeof(struct IntuiText))
  62. #define GSIZE (sizeof(struct Gadget))
  63.  
  64. #define GADGET_SIZE( text ) ( (text)? GTSIZE: GSIZE )
  65.  
  66. struct Gadget    *
  67. CreateGadget( w, h, id, str, sisize )
  68. UBYTE    *str;
  69. {
  70.     UWORD            *gheader;
  71.     struct Gadget    *g;
  72.     struct IntuiText    *it = NULL;
  73.     APTR            sinfo = NULL;
  74.     int                totalsize;
  75.  
  76.     totalsize = GADGET_SIZE( str ) + sizeof ( UWORD * ) + sisize;
  77.     D( printf("CG: w %d, h %d, id %d\n", w, h, id ) );
  78.     D( printf("CG: totalsize = %d\n", totalsize ) );
  79.  
  80.     if ( gheader=(UWORD *) AllocMem((long) totalsize,(long) MEMF_CLEAR))
  81.     {
  82.         *gheader = totalsize;
  83.  
  84.         g = (struct Gadget *) ( gheader + 1 );    /* gadget follow header    */
  85.  
  86.         if ( str )
  87.         {
  88.             it = (struct IntuiText *) ( g + 1 );
  89.  
  90.             initText( it, str );
  91.  
  92.             sinfo = (APTR) (it + 1);    /* follows itext ... */
  93.         }
  94.         else  sinfo = (APTR) (g + 1);    /* or gadget, if no itext */
  95.  
  96.         /* now init the gadget itself    */
  97.         g->Width = w;
  98.         g->Height = h;
  99.  
  100.         g->GadgetText = it;
  101.         g->SpecialInfo = sinfo;
  102.  
  103.         g->GadgetID = id;
  104.     }
  105.     return ( g );
  106. }
  107.  
  108. FreeGadgets( g )
  109. struct Gadget *g;
  110. {
  111.     UWORD            *sizeword;
  112.     struct Gadget    *nextg;
  113.  
  114.     while ( g )
  115.     {
  116.         nextg = g->NextGadget;
  117.  
  118.         sizeword = (UWORD *) g;
  119.         sizeword--;
  120.  
  121.         DF( printf("freeing gadget %lx, size %d\n", g, *sizeword ) );
  122.  
  123.         FreeMem( sizeword, (LONG) *sizeword );
  124.         g = nextg;
  125.     }
  126. }
  127.  
  128. static initText( it, str )
  129. struct IntuiText    *it;
  130. UBYTE            *str;
  131. {
  132.     it->FrontPen = 1;
  133.     it->DrawMode = JAM1;
  134.     it->IText = str;
  135.     it->LeftEdge = 0;
  136.     it->TopEdge = 0;
  137. }
  138.  
  139.